<template>
	<a-drawer
		:title="formData.id ? '编辑轮播图' : '增加轮播图'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="标题：" name="title">
				<a-input v-model:value="formData.title" placeholder="请输入标题" allow-clear/>
			</a-form-item>
			<a-form-item label="图片ID：" name="imgId">
				<a-upload
					v-model:file-list="formData.imgId"
					class="avatar-uploader"
					list-type="picture-card"
					:show-upload-list="false"
					:custom-request="customRequest"
					accept="image/png, image/jpeg, image/jpg"
				>
					<img v-if="imageUrl" :src="imageUrl" alt="avatar"
						 style="max-height: 100px; max-width: 100px"/>

					<div v-else>
						<plus-outlined/>
						<div class="ant-upload-text">上传</div>
					</div>
				</a-upload>
			</a-form-item>
			<a-form-item>
				<a-alert message="请根据设备尺寸上传合适比例的图片" type="warning"/>
			</a-form-item>
			<a-form-item label="适用场景" name="applScene">
				<a-select v-model:value="formData.applScene" placeholder="请选择适用场景" allowClear>
					<a-select-option value="0">通用</a-select-option>
					<a-select-option value="1">H5</a-select-option>
					<a-select-option value="2">柜子屏</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="排序码：" name="sortCode">
				<a-slider v-model:value="formData.sortCode" :max="100"/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</a-drawer>
</template>

<script setup name="bizBannerForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizBannerApi from '@/api/biz/bizBannerApi'
	import sysConfig from '@/config'
	import fileApi from '@/api/dev/fileApi'

	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({ sortCode: 0 })
	const submitLoading = ref(false)
	//图片
	const imageUrl = ref('')

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		formData.value = {
			sortCode: 99
		}
		if (record) {
			let recordData = cloneDeep(record)
			if (record.imgId) {
				//组件是个数组
				recordData.imgId = [record.imgId]
				// 让其回显
				imageUrl.value = sysConfig.API_URL + '/dev/file/download?id=' + record.imgId
			}
			recordData.applScene = record.applScene + ''
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
		imageUrl.value = ''
	}
	// 默认要校验的
	const formRules = {
		title: [required('请输入标题')],
		imgId: [required('请上传图片')],
		applScene: [required('请选择适用场景')],
	}

	const customRequest = (data) => {
		formData.value.imgId = ref([])
		const fileData = new FormData()
		fileData.append('file', data.file)
		fileApi
			.fileUploadReturnId(fileData)
			.then((res) => {
				imageUrl.value = sysConfig.API_URL + '/dev/file/download?id=' + res
				formData.value.imgId.push(res)
				data.onSuccess()
			})

	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				if (formDataParam.imgId) {
					formDataParam.imgId = formDataParam.imgId[0]
				}
				bizBannerApi
					.bizBannerSubmitForm(formDataParam, !formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
